//游戏主角类
class Hero {
    private body:egret.Bitmap;//Hero人物
    private attack:egret.MovieClip;//攻击特效
    private targer;//父类容器
    private protect;//保护罩特效
    private protectLestTime:number = 0;//保护罩的剩余时间
    private isDie:boolean = false;//标示hero是否死亡

    //构造函数//初始化数据
    constructor(target) {
        this.targer = target;
        this.protect = Tool.addBitmap(this.targer, "blood_png", -20, 60, 200, 230, false);
        this.body = Tool.addBitmap(this.targer, "role_" + GameData.hero + "_png", 0, 0, 0, 0, false);
        this.body.x = GameData[GameData.hero].offsetX;
        this.body.y = GameData[GameData.hero].offsetY;
        this.body.scaleX = GameData[GameData.hero].scale;
        this.body.scaleY = GameData[GameData.hero].scale;
        this.protect.visible = false;
        if (GameData.isIntruction == true)this.body.y = 500;
    }

    //站立状态
    public changeStage(type:string):void {
        if (type == "blood") {
            this.protect.visible = true;
            this.protectLestTime = GameData.Attack.blood + GameData.elemNum * GameData.Attack.bloodUp;
            return;
        }

        GameData.attackType = type;
        if (this.attack != null) {
            this.attack.removeEventListener(egret.Event.COMPLETE, this.stand, this);
            this.targer.removeChild(this.attack);
            this.attack = null;
        }
        var data = RES.getRes(type + "_json");
        var txtr = RES.getRes(type + "_png");
        var mcFactory:egret.MovieClipDataFactory = new egret.MovieClipDataFactory(data, txtr);
        this.attack = new egret.MovieClip(mcFactory.generateMovieClipData(type));
        //this.attack.x = 0;
        this.attack.x = 100;
        this.attack.y = 120;
        this.attack.scaleX = 0.5;
        this.attack.scaleY = 0.5;
        if (GameData.isIntruction == true)this.attack.y = 450;
        else if (type == "ice") this.attack.y = 80;
        else if (type == "thunder") this.attack.y = 180;
        else if (type == "fire") {
            this.attack.scaleX = 0.3;
            this.attack.scaleY = 0.3;
            this.attack.y = 150;
            this.attack.x = GameData.tempX + 20;
        }
        else if (type == "wind") {
            this.attack.scaleX = 0.3;
            this.attack.scaleY = 0.3;
            this.attack.y = 130;
        }
        this.attack.play(1);
        this.targer.addChild(this.attack);
        this.attack.addEventListener(egret.Event.COMPLETE, this.stand, this);
    }

    //站立状态
    public stand():void {
        if (this.attack != null) {
            this.attack.removeEventListener(egret.Event.COMPLETE, this.stand, this);
            this.targer.removeChild(this.attack);
            this.attack = null;
        }
    }

    //死亡效果完成
    public die():void {
        GameData.gameOver = true;
        if (this.attack != null) {
            this.attack.removeEventListener(egret.Event.COMPLETE, this.stand, this);
            this.targer.removeChild(this.attack);
            this.attack = null;
        }

        var tw = egret.Tween.get(this.body);
        tw.to({alpha: 0}, 500).call(function () {
            meiriq.CommonComponent.instance.executedHook("gameover",        //函数名
                GameData.score,         //游戏得分
                function () {
                    GameData.saveData();
                    meiriq.CommonComponent.instance.executedHook("restart");
                    UIManage.getInstance().hideGame();
                    //UIManage.getInstance().showGame();
                    UIManage.getInstance().showWelcome();
                },
                this);
        });
    }

    //检测被攻击
    public checkHit():void {
        if (this.protectLestTime > 0) {//若在保护状态
            this.protectLestTime--;
            GameData.heroHit = false;
            return;
        }

        else if (this.protectLestTime == 0) {//保护结束
            this.protect.visible = false;
            this.protectLestTime--;
        }

        if (GameData.heroHit == true && this.isDie != true) {//普通状态下被攻击
            this.die();
            this.isDie = true;
        }
    }
}